package com.tabbinder.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Statistics and cleaning of the database for development service. Is there a
 * standard way to do it? In any case, do <b>NOT</b> upload this class to the
 * production server
 * 
 * @author Tatiana Kichkaylo
 */
public class DbAdminServlet extends HttpServlet {

	/**
	 * In this servlet all operations go through GET, so that we can just type
	 * URL into a browser
	 */
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		// Get the desired action
		Object action = req.getParameter("action");
		if (action == null)
			action = "help";

		PrintWriter out = resp.getWriter();

		// Do as asked
		if ("help".equals(action)) {
			doHelp(out);
		} else if ("stat".equals(action)) {
			doHelp(out);
			out.append("\n");
			doStat(resp);
		} else if ("clear".equals(action)) {
			doClear(resp);
		} else {
			out.append("Unsupported operation " + action + "\n");
			doHelp(out);
		}
	}

	private void doHelp(PrintWriter out) {
		out.append("Supported argument: action; values: help, stat, clear");
	}

	@SuppressWarnings("unchecked")
	private void doStat(HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();

			Query query = pm.newQuery("select from "
					+ BindRecord.class.getName());
			List<BindRecord> records = (List<BindRecord>) query.execute();
			out.append("Count for BindRecord: " + records.size() + "\n");

			query = pm.newQuery("select from " + Binder.class.getName());
			List<Binder> binders = (List<Binder>) query.execute();
			out.append("Count for Binder: " + binders.size() + "\n");
		} catch (Exception e) {
			out.append("Got an error\n" + e + "\n");
		} finally {
			// Rollback is a NOOP for read-only
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	private void doClear(HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();

			Query query = pm.newQuery("select from "
					+ BindRecord.class.getName());
			List<BindRecord> records = (List<BindRecord>) query.execute();
			int count = records.size();
			if (count > 0)
				pm.deletePersistentAll(records);
			out.append("Deleted " + count + " BindRecords\n");

			query = pm.newQuery("select from " + Binder.class.getName());
			List<Binder> binders = (List<Binder>) query.execute();
			count = binders.size();
			if (count > 0)
				pm.deletePersistentAll(binders);
			out.append("Deleted " + count + " Binders\n");
			tx.commit();
		} catch (Exception e) {
			out.append("Got an error\n" + e + "\n");
		} finally {
			pm.close();
		}
	}
}
